﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Pages
{
    public partial class GPSTrackingWebsiteManager : System.Web.UI.MasterPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

                if (objSessionManager != null)
                {
                    string CompanyID = string.Empty;
                    MenuMap.Items[0].Text = "Bienvenido(a): " + HttpContext.Current.User.Identity.Name;

                    if (objSessionManager.IsMaster)
                    {
                        System.Data.DataTable dtMenu = GPSTrackingDBV2Custom.Data.DataRepository.MenuProvider.GetMenuByMasterUsername(objSessionManager.MasterUsername, 2);

                        Build_Menu(dtMenu);
                    }
                    else
                    {
                        if (objSessionManager.IsAdmin)
                        {
                            System.Data.DataTable dtMenu = GPSTrackingDBV2Custom.Data.DataRepository.MenuProvider.GetMenuByAdminUsername(objSessionManager.AdminUsername, objSessionManager.CompanyIDSelectedComboBox, 2);

                            Build_Menu(dtMenu);
                            
                        }
                        else
                        {
                            if (objSessionManager.IsUser)
                            {
                                System.Data.DataTable dtMenu = GPSTrackingDBV2Custom.Data.DataRepository.MenuProvider.GetMenuByUsername(objSessionManager.Username, objSessionManager.CompanyID, 2);

                                Build_Menu(dtMenu);
                            }
                        }
                    }
                }
                else
                {
                    System.Web.Security.FormsAuthentication.SignOut();
                    Session.Abandon();

                    if (Request.Cookies["GUID"] != null)
                        Response.Redirect("../../" + Request.Cookies["Login"].Value + ".aspx?GUID=" + Request.Cookies["GUID"].Value, true);
                    else
                        Response.Redirect("../../" + Request.Cookies["Login"].Value + ".aspx", true);
                }
            }
        }

        private void Build_Menu(System.Data.DataTable dataTable)
        {           
            IEnumerable<DataRow> menuRows = from p in dataTable.AsEnumerable() select p;

            IEnumerable<DataRow> menuParents = menuRows.Where(x => x.Field<int?>("Parent") == null).OrderBy(x => x.Field<short>("BySequence"));

            foreach (DataRow row in menuParents)
            {
                Telerik.Web.UI.RadMenuItem menuItem = new Telerik.Web.UI.RadMenuItem();
                menuItem.ImageUrl = "../../Template/Icons/16px/" + row.Field<string>("ImageUrl");
                menuItem.Value = row.Field<string>("DirectoryPath") + row.Field<string>("Page");
                if (row.Field<int>("TypeModuleID") == 5 || row.Field<int>("TypeModuleID") == 4) menuItem.NavigateUrl = row.Field<string>("DirectoryPath") + row.Field<string>("Page");
                menuItem.Text = row.Field<string>("Name");
                menuItem.ToolTip = row.Field<string>("ToolTip");
                menuItem.Attributes.Add("Width", Convert.ToString(row.Field<short?>("Width")));
                menuItem.Attributes.Add("Height", Convert.ToString(row.Field<short?>("Heigth")));
                menuItem.Attributes.Add("PosX", Convert.ToString(row.Field<short?>("PositionX")));
                menuItem.Attributes.Add("PosY", Convert.ToString(row.Field<short?>("PositionY")));
                menuItem.Attributes.Add("Page", Convert.ToString(row.Field<string>("Page")));

                IEnumerable<DataRow> menuChilds = menuRows.Where(x => x.Field<int?>("Parent") == row.Field<int>("MenuID")).OrderBy(x => x.Field<short>("BySequence"));

                foreach (DataRow rowChild in menuChilds)
                {
                    Telerik.Web.UI.RadMenuItem menuItemChild = new Telerik.Web.UI.RadMenuItem();
                    menuItemChild.ImageUrl = "../../Template/Icons/16px/" + rowChild.Field<string>("ImageUrl");
                    menuItemChild.Value = rowChild.Field<string>("DirectoryPath") + rowChild.Field<string>("Page");
                    if (rowChild.Field<int>("TypeModuleID") == 5 || rowChild.Field<int>("TypeModuleID") == 4) menuItemChild.NavigateUrl = rowChild.Field<string>("DirectoryPath") + rowChild.Field<string>("Page");
                    menuItemChild.Text = rowChild.Field<string>("Name");
                    menuItemChild.ToolTip = rowChild.Field<string>("ToolTip");
                    menuItemChild.Attributes.Add("Width", Convert.ToString(rowChild.Field<short?>("Width")));
                    menuItemChild.Attributes.Add("Height", Convert.ToString(rowChild.Field<short?>("Heigth")));
                    menuItemChild.Attributes.Add("PosX", Convert.ToString(rowChild.Field<short?>("PositionX")));
                    menuItemChild.Attributes.Add("PosY", Convert.ToString(rowChild.Field<short?>("PositionY")));
                    menuItemChild.Attributes.Add("Page", Convert.ToString(rowChild.Field<string>("Page")));

                    menuItem.Items.Add(menuItemChild);
                }

                MenuMap.Items.Add(menuItem);
            }

        }

        protected void btnLoginOut_Click(object sender, EventArgs e)
        {
            System.Web.Security.FormsAuthentication.SignOut();
            Session.Abandon();

            if (Request.Cookies["GUID"] != null)
                Response.Redirect("../../" + Request.Cookies["Login"].Value + ".aspx?GUID=" + Request.Cookies["GUID"].Value, true);
            else
                Response.Redirect("../../" + Request.Cookies["Login"].Value + ".aspx", true);
        }
    }
}